package com.ruoyi.bean.controller;

import cn.hutool.core.util.ObjectUtil;
import com.ruoyi.bean.service.LoginService;
import com.ruoyi.common.result.Result;
import com.ruoyi.domain.dto.LoginBody;
import com.ruoyi.domain.vo.user.LoginUser;
import com.ruoyi.domain.vo.user.UserInfoVo;
import io.github.stylesmile.annotation.AutoWired;
import io.github.stylesmile.annotation.Controller;
import io.github.stylesmile.annotation.RequestBody;
import io.github.stylesmile.annotation.RequestMapping;
import io.github.stylesmile.request.RequestMethod;
import io.github.stylesmile.server.Request;
import io.github.stylesmile.tool.JsonGsonUtil;

/**
 * 登录验证
 *
 * @author ruoyi
 */
@Controller
public class LoginController {
    @AutoWired
    private LoginService loginService;
//    private SysLoginService loginService;
//    private SysLoginServiceImpl loginService;

//    @AutoWired
//    private SysMenuServiceImpl menuService;

    //    @AutoWired
//    private SysPermissionService permissionService;
//    @AutoWired
//    private SessionService sessionService;

    /**
     * 登录方法
     *
     * @param loginBody 登录信息
     * @return 结果
     */
//    @PostMapping("/login")
    @RequestMapping(value = "/auth/login", method = RequestMethod.POST)
    public Result login(@RequestBody String body, Request request) {
        LoginBody loginBody = JsonGsonUtil.jsonToObject(body, LoginBody.class);

        // 生成令牌
//        return loginService.login(loginBody.getUsername(), loginBody.getPassword(), loginBody.getCode(),
//        return loginService.login(loginBody.getUsername(), loginBody.getPassword(), loginBody.getCode(),
//                loginBody.getUuid(), request);
        return null;
    }

    /**
     * 获取用户信息
     *
     * @return 用户信息
     */
    @RequestMapping("/getInfo")
    public Result<UserInfoVo> getInfo() {
        UserInfoVo userInfoVo = new UserInfoVo();
        LoginUser loginUser = LoginHelper.getLoginUser();
        if (TenantHelper.isEnable() && LoginHelper.isSuperAdmin()) {
            // 超级管理员 如果重新加载用户信息需清除动态租户
            TenantHelper.clearDynamic();
        }
        SysUserVo user = userService.selectUserById(loginUser.getUserId());
        if (ObjectUtil.isNull(user)) {
            return R.fail("没有权限访问用户数据!");
        }
        userInfoVo.setUser(user);
        userInfoVo.setPermissions(loginUser.getMenuPermission());
        userInfoVo.setRoles(loginUser.getRolePermission());
        return R.ok(userInfoVo);
    }

    /**
     * 获取路由信息
     *
     * @return 路由信息
     */
//    @GetMapping("getRouters")
//    @RequestMapping("getRouters")
//    public AjaxResult getRouters() {
////        Long userId = SecurityUtils.getUserId();
//        Long userId = SessionService.getSession().getUserId();
//        List<SysMenu> menus = menuService.selectMenuTreeByUserId(userId);
//        return AjaxResult.success(menuService.buildMenus(menus));
//    }
}
